package com.itheima.person.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Aspect1 {

    private static final Logger log = LoggerFactory.getLogger(Aspect1.class);

    // 增强方法逻辑
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        long start = System.nanoTime();

        log.debug(pjp.getSignature() + "开始执行");

        // pjp.proceed() 即表示调用目标方法（核心方法|被增强方法），或下一个通知
        Object result = pjp.proceed();

        long end = System.nanoTime();

        log.debug(pjp.getSignature() + "完毕执行");
        log.debug(pjp.getSignature() + "执行耗时: {} nm", (end - start));
        return result;
    }
    public void beforeCall(JoinPoint jp) {
        log.debug(jp.getSignature() + "方法，开始执行");
    }

    public void afterCall(JoinPoint jp) {
        log.debug(jp.getSignature() + "方法，执行结束,释放资源");
    }

    public void afterReturningCall(JoinPoint jp, Object result) {
        log.debug(jp.getSignature() + "方法，执行结束,返回值:"+result);
    }

    public void afterThrowingCall(JoinPoint jp, Throwable ex) {
        log.error(jp.getSignature() + "方法，执行发生异常" + ex.getClass().getName(), ex);
    }
}